
import {
  getServAlertApi,
  saveServAlertApi,
  updateServAlertApi,
  getAlertRepTypeApi,
  updateDeletedApi,
  getAlertTypeApi,
  getRepReportParamLabelListApi
} from '@/services/alertConfig.js';
// import { getEventListAllApi } from '@/services/fraMonitoring.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    servAlertLoading: false,
    servAlertList: [],
    saveServAlertLoading: false,
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    alertRepType: [],
    alertType: [],
    updateDeletedLoading: false,
    rpPaginationOptions: {
      pageNum: 1,
      pageSize: 5,
      total: 0
    },
    repReportParamLabelLoading: false,
    repReportParamLabelList: []
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取列表
    async getServAlert({ commit, state, dispatch }, params) {
      commit('setState', { key: 'servAlertLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getServAlertApi(apiParams);
      dispatch('setting/setAcarsTime', {}, { root: true });
      commit('setState', { key: 'servAlertList', payload: rep.data.list });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'servAlertLoading', payload: false });
    },
    // 获取标签列表
    async getRepReportParamLabelList({ commit, state }, params) {
      commit('setState', { key: 'repReportParamLabelLoading', payload: true });
      const { pageNum, pageSize } = state.rpPaginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getRepReportParamLabelListApi(apiParams);
      commit('setState', { key: 'repReportParamLabelList', payload: rep.data.list });
      commit('setState', { key: 'rpPaginationOptions', payload: { ...state.rpPaginationOptions, total: rep.data.total }});
      commit('setState', { key: 'repReportParamLabelLoading', payload: false });
    },
    async getAlertType({ commit, state }) {
      const rep = await getAlertTypeApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'alertRepType', payload: newData });
    },
    async getAlertRepType({ commit, state }) {
      const rep = await getAlertRepTypeApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'alertType', payload: newData });
    },
    // 删除
    async updateDeleted({ commit }, params) {
      commit('setState', { key: 'updateDeletedLoading', payload: true });
      try {
        const rep = await updateDeletedApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'updateDeletedLoading', payload: false });
      }
    },
    // 新增
    async saveServAlert({ commit }, params) {
      commit('setState', { key: 'saveServAlertLoading', payload: true });
      try {
        const rep = await saveServAlertApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'saveServAlertLoading', payload: false });
      }
    },
    // 修改
    async updateServAlert({ commit }, params) {
      commit('setState', { key: 'saveServAlertLoading', payload: true });
      try {
        const rep = await updateServAlertApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'saveServAlertLoading', payload: false });
      }
    }
  }
};
